function showTranslatedObjectsByVelocityField(maskfilenames,velocityfield,slice)
%slice(1)=31;
%slice(2)=32;
%clear mask1 mask2 CurrentObjects1 CurrentObjects2 transObjects transmask rgb1 rgb2 transRgb
binSize=8;
minX=16;
minY=16;
BinXLim=size(velocityfield{1}{5},2);
BinYLim=size(velocityfield{1}{6},1);
%velocityfield{i}={x,y,u,v,fu,fv};
     

%output_write_seg = 'C:\Users\Michael Ferry\experiments\MF220\analysis_data\xy2\segmented\';
%segfilelist = dir([output_write_seg 'segment*']);
%segfilenames = {segfilelist.name}';

%mask1=~imread([output_write_seg segfilenames{slice(1)}]);
%mask1=~imread([maskdir maskfilenames{slice(1)}]);
mask1=~imread(maskfilenames{slice(1)});
CurrentObjects1 = regionprops(bwconncomp(mask1,4),'Centroid','PixelList','PixelIdxList');
mask1=uint16(mask1);

%mask2=~imread([output_write_seg segfilenames{slice(2)}]);
%mask2=~imread([maskdir maskfilenames{slice(2)}]);
mask2=~imread(maskfilenames{slice(2)});
CurrentObjects2 = regionprops(bwconncomp(mask2,4),'Centroid','PixelList','PixelIdxList');
%mask2=uint16(mask2);



transMask=uint16(zeros(size(mask1)));

% for i=1:length(CurrentObjects1(1).PixelList)
%     mask1(CurrentObjects1(1).PixelList(i,2),CurrentObjects1(1).PixelList(i,1))=2;
% end
%mask1(CurrentObjects1(1).PixelIdxList)=2;



%Centroid(1)=Centroid(1)-minX/2;
%Centroid(2)=Centroid(2)-minY/2;
%transObjects=zeros(size(CurrentObjects1));
transObjects(size(CurrentObjects1)) = struct('PixelList', []);

for j=1:length(CurrentObjects1)
%j=1103;
%CurrentObjects1(j)
    Centroid=CurrentObjects1(j).Centroid;

     mask1(CurrentObjects1(j).PixelIdxList)=mod(j,255);

    BinX = (Centroid(1)-minX/2)/binSize;
    BinY = (Centroid(2)-minY/2)/binSize;
    if((BinX > 1 && BinX <= BinXLim) && (BinY > 1 && BinY <= BinYLim))
        
    BinXC = [floor(BinX),ceil(BinX)];
    BinYC = [floor(BinY),ceil(BinY)];
    
    subU=velocityfield{slice(1)}{5}(BinYC,BinXC);
    subV=velocityfield{slice(1)}{6}(BinYC,BinXC);
    
    normBinX=BinX-floor(BinX);
    normBinY=BinY-floor(BinY);
    
    u = [1-normBinY,normBinY]*subU*[1-normBinX;normBinX];
    v = [1-normBinY,normBinY]*subV*[1-normBinX;normBinX];

    transObjects(j).PixelList(:,1)=max(round(CurrentObjects1(j).PixelList(:,1)+u),1);
    transObjects(j).PixelList(:,2)=max(round(CurrentObjects1(j).PixelList(:,2)+v),1);
    
    %transMask(transObjects.PixelList(:,1),transObjects.PixelList(:,2))=1;
    
    for i=1:size(transObjects(j).PixelList,1)
        transMask(transObjects(j).PixelList(i,2),transObjects(j).PixelList(i,1))=mod(j,255);
    end
    
    else
        for i=1:size(transObjects(j).PixelList,1)
        transMask(transObjects(j).PixelList(i,2),transObjects(j).PixelList(i,1))=40;
        end
    end
        
end
rgb1=label2rgb(mask1,'hsv','k','shuffle');
%rgb2=label2rgb(mask2,'hsv','k');
rgb2=mask2;
transRgb=label2rgb(transMask,'hsv','k','shuffle');

figure, imshow(rgb1,'InitialMagnification','fit')
title('Field frame i');

figure, imshow(transRgb,'InitialMagnification','fit')
title('Field frame i translated');

figure, imshow(rgb1,'InitialMagnification','fit'), hold on
himage = imshow(transRgb,'InitialMagnification','fit');
set(himage, 'AlphaData', 0.3);
title('Field frame i with translated');

figure, imshow(rgb2,'InitialMagnification','fit'), hold on
himage = imshow(transRgb,'InitialMagnification','fit');
set(himage, 'AlphaData', 0.3);
title('Field frame i+1 with translated');

figure, imshow(rgb2,'InitialMagnification','fit'), hold on
himageRgb2=imshow(rgb1,'InitialMagnification','fit');
set(himageRgb2, 'AlphaData', 0.3);
% himage = imshow(transRgb,'InitialMagnification','fit');
% set(himage, 'AlphaData', 0.3);
title('Field frame i,i+1 with translated');

